package com.bdzl.auth.controller;


import com.bdzl.auth.util.ImageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @Author liu-miss
 * @Description //TODO 图片验证码Controller
 * @Date 11:27 2021/5/11
 * @Param
 * @return
 **/
@Controller
@RequestMapping("/imageValidate")
public class ImageValidateController {
    //保存验证图片信息
    public static String KAPTCHA_SESSION_KEY = "KAPTCHA_SESSION_KEY";


    /**
     * @Author Zhn
     * @Description //TODO 验证图片验证码的接口
     * @Date 11:27 2021/5/11
     * @Param [x, request]  x 验证码坐标
     * @return boolean
     **/
    @ResponseBody
    @RequestMapping("/check")
    public boolean check(@RequestParam(value = "x",required = true) Integer x, HttpServletRequest request){
        HttpSession session = request.getSession();
        Object checkX = session.getAttribute(KAPTCHA_SESSION_KEY);
        if(checkX ==null){
            return false;
        }
        Integer outX = Integer.valueOf(checkX.toString());
        if(Math.max(x, (outX-5)) == Math.min(x, (outX+5))){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取图片验证码的接口
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/imageShow")
    public Map<String,Object> imageShow( HttpServletRequest request) throws Exception {
        HttpSession session = request.getSession();
        Random rand = new Random() ;
        int min = 80;
        int max = 160;
        int min2 = 10;
        int max2 = 70;
        //判断使用的x轴
        Integer rx = rand.nextInt(max-min)+min;
        //判断使用的y轴
        Integer ry = rand.nextInt(max2-min2)+min2;
        //随机的原图片
        BufferedImage image = ImageUtil.Image();
        //获取剪切图
        BufferedImage checkImgMin= new BufferedImage(ImageUtil.targetLength, ImageUtil.targetWidth, BufferedImage.TYPE_4BYTE_ABGR);
//        BufferedImage checkImgMin = ImageUtil.getMarkImage(image,rx,ry);
        //获取加阴影图片
        ImageUtil.cutByTemplate(image,checkImgMin,ImageUtil.getBlockData(),rx,ry);
//        BufferedImage checkImg = ImageUtil.cutByTemplate(image,ImageUtil.getCutAreaData(image.getWidth(),image.getHeight(),rx,ry,55,55));
        //剪切图添加红色边框
//        BufferedImage frameMin = ImageUtil.imagesFrameMin(checkImgMin);
//        BufferedImage frame = ImageUtil.imagesFrame(image,rx,ry,55,55);
        image = ImageUtil.simpleBlur(image,null);
        checkImgMin = ImageUtil.simpleBlur(checkImgMin,null);
        String data1 = base64Image(checkImgMin);
        String data2 = base64Image(image);

        Map<String,Object> map = new HashMap<>();
        map.put("y",ry);
//        map.put("x",rx);
        session.setAttribute(KAPTCHA_SESSION_KEY, rx);
        map.put("checkImgMin",data1);
        map.put("checkImg",data2);
        return map;
    }

    private String base64Image(BufferedImage image) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
        ImageIO.write(image, "png", baos);//写入流中
        byte[] bytes = baos.toByteArray();//转换成字节
        BASE64Encoder encoder = new BASE64Encoder();
        String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
        png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
        return "data:image/jpg;base64," + png_base64;
    }
}
